package org.wzq.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.wzq.biz.Topic;

import java.util.List;

/**
 * project: spring-mvc-forum
 * ate: 14-9-17
 * author: wzq
 * description: TODO
 */
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class TopicDao {
    @Autowired
    private SessionFactory sessionFactory;


    @Transactional
    public List<Topic> getTopicBySectionId(String sectionId) {
        String hql = "from Topic where sectionId=? and pTopicId < 0";
        Query query = sessionFactory.getCurrentSession().createQuery(hql).setString(0, sectionId);
        List<Topic> list = query.list();
        return list;
    }

    @Transactional
    public Topic getTopicById(String topicId) {
        String hql = "from Topic where topicId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, topicId);
        return (Topic) query.uniqueResult();
    }

    @Transactional
    public List<Topic> getReplyById(String topicId) {
        String hql = "from Topic where pTopicId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, topicId);
        List<Topic> list = query.list();
        return list;
    }


    @Transactional
    public void saveOrUpdate(Topic topic) {
        sessionFactory.getCurrentSession().saveOrUpdate(topic);
    }

    public static final int PAGE_ROW_NUM = 5;

    @Transactional
    public List<Topic> getTopicBySectionIdAndPage(String sectionId, int page, int maxPage) {
        String hql = "from Topic where sectionId=? and pTopicId =-1 order by lastModifiedTime desc";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        if (page < 1) {
            page = 1;
        } else if (page > maxPage) {
            page = maxPage;
        }
        page -= 1;
//        page = page < 0 ? 0 : page;
//        page = page > maxPage ? maxPage : page;
        query.setFirstResult(page * PAGE_ROW_NUM);
        query.setMaxResults(PAGE_ROW_NUM);

        query.setString(0, sectionId);
        List<Topic> list = query.list();
        return list;
    }

    @Transactional
    private int getTopicNum(String sectionId) {
//        Query query = sessionFactory.getCurrentSession()
//                .createQuery("select count(*) from Topic where pTopicId=-1 and sectionId=" + sectionId);
        Query query = sessionFactory.getCurrentSession()
                .createQuery("select count(*) from Topic where pTopicId=-1 and sectionId=:sectionId");
        query.setString("sectionId", sectionId);
        int rowNum = ((Long) query.uniqueResult()).intValue();
        return rowNum;
    }

    @Transactional
    public int getMaxPageNum(String sectionId) {
        int total = getTopicNum(sectionId);
        return (total + PAGE_ROW_NUM - 1) / PAGE_ROW_NUM;
    }
}
